﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace HOOMT
{
    class RFormToDiagramDAO:BaseDAO
    {
        OriginalDiagramDAO diagramDBO = new OriginalDiagramDAO();
        public int AddRelations(OriginalModelForm omf)
        {
            try
            {

                this.DeleteRelations(omf);
                foreach(OriginalDiagram diagram in omf.diagramList)
                {
                    SQLiteCommand cmd1 = this.GetCmd();
                    cmd1.CommandText = "insert into r_form_diagram(id,formid,diagramid,formType,diagramtype) " +
                        " values (@id,@formid,@diagramid,@formType,@diagramtype)";
                    cmd1.Parameters.Add(new SQLiteParameter("id",System.Guid.NewGuid()));
                    cmd1.Parameters.Add(new SQLiteParameter("formid", omf.id));
                    cmd1.Parameters.Add(new SQLiteParameter("diagramid", diagram.Id));
                    cmd1.Parameters.Add(new SQLiteParameter("formType", omf.GetType().ToString()));
                    cmd1.Parameters.Add(new SQLiteParameter("diagramtype", diagram.GetType().ToString()));
                    cmd1.ExecuteNonQuery();

                    diagramDBO.AddDiagram(diagram);
         
                }
                return omf.diagramList.Count;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return 0;
            }
            finally
            {
                CloseConnection();
            }

        }

        public int DeleteRelations(OriginalModelForm omf)
        {
            try
            {
                SQLiteCommand cmd1 = this.GetCmd();
                cmd1.CommandText = "delete from  r_form_diagram where formid=@formid";
                cmd1.Parameters.Add(new SQLiteParameter("formid", omf.id));
                int i = cmd1.ExecuteNonQuery();
                foreach (OriginalDiagram diagram in omf.diagramList)
                {
                    diagramDBO.DeleteDiagram(diagram);
                }
                return omf.diagramList.Count;

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return 0;
            }
            finally
            {
                CloseConnection();
            }
        }

        public List<OriginalDiagram> GetRelations(OriginalModelForm omf)
        {
            try
            {
                List<OriginalDiagram> list = new List<OriginalDiagram>();
                SQLiteCommand cmd = GetCmd();
                cmd.CommandText = "select * from r_form_diagram where formid=@formid";
                cmd.Parameters.Add(new SQLiteParameter("formid", omf.id));
                SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    String diagramId = reader["diagramId"].ToString();
                    list.Add(diagramDBO.GetDiagramById(diagramId));
             //       Console.WriteLine("zfzfzfzf");
                }
            //    Console.WriteLine(list.Count);
                return list;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
            finally
            {
                CloseConnection();
            }
        }
    }
}
